package com.dfrz.household.controller;

import com.dfrz.household.bean.ShopCart;
import com.dfrz.household.bean.User;
import com.dfrz.household.service.IShopCartService;
import com.dfrz.household.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
public class ShopCartController {
    @Autowired
    IShopCartService  shopCartService;
    //添加购物车
    @RequestMapping("/insertshopcart")
    public Result insertShopCart(ShopCart cart){
        Result result = new Result();
        int count = shopCartService.shopCart_insert(cart);
        if (count>0){
            result.setCode(0);
            result.setMsg("添加成功");
        }
        return result;
    }
    //回显购物车
    @RequestMapping("/showcarts")
    public ModelAndView showCart(HttpSession session){
        ModelAndView mv = new ModelAndView();
        User user= (User) session.getAttribute("login_user");
        if(user!=null){
            List<ShopCart> list=shopCartService.getShopCartsByUid(user.getUid());
            mv.addObject("carts",list);
            mv.setViewName("order");
        }
        return mv;
    }
    //删除购物车
    @RequestMapping("/deletecart")
    public Result deleteCart(Integer cid){
        Result result = new Result();
        int count=shopCartService.deleteShopCart(cid);
        if (count>0){
            result.setCode(0);
            result.setMsg("删除成功");
        }
        return result;
    }
    //更新购物车
    @RequestMapping("/updatecart")
    public Result updateCart(ShopCart cart,HttpSession session){
        Result result = new Result();
        User user= (User) session.getAttribute("login_user");
        if(user!=null){
            cart.setUid(user.getUid());
            int count=shopCartService.updateShopCart(cart);
            if (count>0){
                result.setCode(0);
            }
        }

        return result;
    }
}
